$(document).ready(function () {

    (function ($) {
        "use strict";


        jQuery.validator.addMethod('answercheck', function (value, element) {
            return this.optional(element) || /^\bcat\b$/.test(value)
        }, "type the correct answer -_-");

        // validate contactForm form
        $(function () {
            $('#contactForm').validate({
                rules: {
                    name: {
                        required: true,
                        minlength: 1
                    },
                    subject: {
                        required: true,
                        minlength: 1
                    },
                    email: {
                        required: true,
                        email: true
                    },
                    message: {
                        required: true,
                        minlength: 5
                    }
                },
                messages: {
                    name: {
                        required: "拜托你需要一个名字",
                        minlength: "拜托你需要一个名字"
                    },
                    subject: {
                        required: "写写标题",
                        minlength: "写写标题"
                    },
                    email: {
                        required: "没有邮箱怎么回你呢"
                    },
                    message: {
                        required: "你至少要写几个字对吧",
                        minlength: "可以多些点么"
                    }
                },
                submitHandler: function (form) {
                    let name = $("#name");
                    let email = $("#email");
                    let subject = $("#subject");
                    let message = $("#message");
                    if (name == null || name.val() == "") {
                        alert("名字不能为空");
                        return;
                    }
                    if (name.val().length > 32) {
                        alert("名字不能超过 32 个字符");
                        return;
                    }
                    if (email == null || email.val() == "") {
                        alert("邮箱不能为空");
                        return;
                    }
                    if (email.val().length > 128) {
                        alert("邮箱不能超过 128 个字符");
                        return;
                    }
                    if (subject == null || subject.val() == "") {
                        alert("主题不能为空");
                        return;
                    }
                    if (subject.val().length > 64) {
                        alert("主题不能超过 64 个字符");
                        return;
                    }
                    if (message == null || message.val() == "") {
                        alert("消息不能为空");
                        return;
                    }
                    if (message.val().length > 512) {
                        alert("消息不能超过 512 个字符");
                        return;
                    }
                    $(form).ajaxSubmit({
                        url: "contact/add",
                        type: "POST",
                        data: $(form).serialize(),
                        success: function () {
                            $('#contactForm :input').attr('disabled', 'disabled');
                            $('#contactForm').fadeTo("slow", 1, function () {
                                $(this).find(':input').attr('disabled', 'disabled');
                                $(this).find('label').css('cursor', 'default');
                                $('#success').fadeIn()
                                $('.modal').modal('hide');
                                $('#success').modal('show');
                            })
                        },
                        error: function () {
                            $('#contactForm').fadeTo("slow", 1, function () {
                                $('#error').fadeIn()
                                $('.modal').modal('hide');
                                $('#error').modal('show');
                            })
                        }
                    })
                }
            })
        })

    })(jQuery)
})